<template>
  <div class="home-container">
    <!-- 顶部搜索框 -->
    <van-nav-bar class="page-nav-bar" fixed="">
      <van-button
        class="search-btn"
        slot="title"
        icon="search"
        round
        size="small"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- channels频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :title="item.name" :key="item.id">
        <ArticleList :channel="item" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="gengduo-bth"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <van-popup
      v-model="isChennelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <ChannelEdit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/channel";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
import ChannelEdit from "./components/channel-edit.vue";
import ArticleList from "./components/article-list.vue";
export default {
  name: "HomeIndex",
  data() {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false,
    };
  },
  components: {
    ArticleList,
    ChannelEdit,
  },
  methods: {
    async loadChannels() {
      const channel = getItem("TOUTIAO_CHANNELS");
      try {
        if (this.user || !channel) {
          const res = await getUserChannels();
          this.channels = res.data.data.channels;
        } else {
          this.channels = channel;
        }
      } catch (e) {
        this.$toast("用户频道获取失败");
      }
    },
    onUpdateActive(index, isChennelEditShow = true) {
      this.active = index;
      this.isChennelEditShow = isChennelEditShow;
    },
  },
  computed: {
    ...mapState(["user"]),
  },
  created() {
    this.loadChannels();
  },
};
</script>

<style lang="less" scoped>
.home-container {
  padding-bottom: 98px;
  padding-top: 174px;
  .van-nav-bar__title {
    max-width: none;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    color: #fff;
    .van-button__icon {
      color: #fff;
      font-size: 32px;
    }
  }

  /deep/ .channel-tabs {
    .van-tabs__wrap {
      //这里只需要给父节点channel-tabs添加/deep/,这样下边的子节点都会生效
      position: fixed;
      left: 0;
      right: 0;
      z-index: 1;
      top: 92px;
      height: 82px;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 28px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
      .van-tabs__line {
        width: 31px !important;
        height: 6px;
        background-color: #3296fa;
        bottom: 8px;
      }
    }
    .gengduo-bth {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      line-height: 82px;
      background-color: #fff;
      text-align: center;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &::before {
        content: "";
        position: absolute;
        left: 0;
        width: 2px;
        height: 82px;
        background: url("~@/assets/gradient-gray-line.png");
        background-size: contain;
      }
    }
    .placeholder {
      flex-shrink: 0; //此元素不参与flex的
      width: 66px;
      height: 82px;
    }
  }
}
</style>>